<?php
class RepayAction extends CommonAction
{
	public function index()
	{
		$userModel = D("User");
		$userInfo = $this->isLogin();
		$doquota = $userModel->getDoquota($userInfo["id"]);
		$dkmony=$userModel->getUserDMoney($userInfo["id"]);
		$qianbao=$userModel->getInfo("id", $userInfo["id"],'qianbao');
		$this->assign("doquota", number_format($doquota-$dkmony,2));
		$this->assign("dkmony", number_format($dkmony,2));
		$this->assign("qianbao", number_format($qianbao),2);
		$this->assign("qianbao1", $qianbao);
		$tmpTime = strtotime("+29 day", time());
		$WalletModel = D("Wallet");
		$wallet = $WalletModel->where(array("uid" => $userInfo["id"], "status" => 0))->find();
		$wallet1 = $WalletModel->where(array("uid" => $userInfo["id"]))->find();
		//$this->assign("money", toMoney($bill["money"] + $bill["interest"] + $bill["overdue"]));
		 
		 
		//$list = $loanorderModel->getNoneList($userInfo["id"]);
			$loanorderModel = D("Loanorder");
			$loanInfo = $loanorderModel->where(array("uid" => $userInfo["id"]))->relation(true)->find();
	 
	
		$loanData = json_decode($loanInfo["data"], true);
		$this->assign("money", toMoney($bill["money"]+ $bill["overdue"]));
		$this->assign("time", $bill["repayment_time"]?$bill["repayment_time"]:strtotime('now'));
		
		$this->assign("billId", $bill["id"]);
		$this->assign("wallet", $wallet);
		$this->assign("wallet1", $wallet1);
		$this->assign("loanData", $loanData);
		$this->assign("loanInfo", $loanInfo);
		$this->assign("user", $userInfo);
	 
		$this->display();
	}
	public function pay(){
	    $this->display();
	}
	public function order()
	{
		$loanorderModel = D("Loanorder");
		$userInfo = $this->isLogin();
		//$list = $loanorderModel->getNoneList($userInfo["id"]);
		$list=$loanorderModel->where(array("uid" => $userInfo["id"]))->find();
		if(empty($list)){
		//	$this->error("暂时无需还款！");
		}
		
		//利息 = $list['money'] * 利息 * 期限
		$hasMoney=floatval($list['money'])*floatval($list['interest'])*intval($list['time']);
		
		$hasMoney=number_format($hasMoney+floatval($list['money']),2);
		
		$this->assign("oldMoney", number_format($list['money']));
		//$list['money']=number_format($list['money'],2);
		$loanbillModel = D("Loanbill");
		//$hasMoney = $loanbillModel->where(array("uid" => $userInfo["id"], "status" => array("in", "0,1")))->sum("money");
		//$hasInterest = $loanbillModel->where(array("uid" => $userInfo["id"], "status" => array("in", "0,1")))->sum("interest");
		//$hasOverdue = $loanbillModel->where(array("uid" => $userInfo["id"], "status" => array("in", "0,1")))->sum("overdue");
		//$this->assign("noneMoney", toMoney($hasMoney + $hasInterest + $hasOverdue));
		
		//$hasMoney=
		$this->viewContracts();
		$this->assign("noneMoney", $hasMoney);
		$this->assign("list", $list);
		$this->display();
	}
	public function viewContracts(){
	    
	    	$userInfo = $this->isLogin();
		$id = $userInfo['id'];
		$loanorderModel = D("Loanorder");
		$loanInfo = $loanorderModel->where(array("uid" => $id))->relation(true)->find();
	 
		$this->assign("data", $loanInfo);
		$contractTpl = C("contractTpl");
		$contractTpl = empty($contractTpl) ? "" : htmlspecialchars_decode(htmlspecialchars_decode($contractTpl));
		$loanData = json_decode($loanInfo["data"], true);
		$timeType = $loanInfo["timetype"] == 1 ? "月" : "日";
		if ($timeType == "月") {
			$endTime = strtotime("+" . intval($loanInfo["time"]) . " Month", $loanInfo["start_time"]);
		} else {
			$endTime = strtotime("+" . intval($loanInfo["time"]) . " Day", $loanInfo["start_time"]);
		}
		$sign = "<img src='" . $loanInfo["sign"] . "' width='110px' />";
		 
		$infoModel = D("Info");
		$userInfo = $infoModel->getAuthInfo($loanInfo["uid"]);
		$addessInfo = json_decode($userInfo["addess"], true);
		$contractTpl = str_replace("｛借款人名称｝", $loanInfo["name"], $contractTpl);
		$contractTpl = str_replace("｛借款人身份证号｝", $loanData["idcard"], $contractTpl);
		$contractTpl = str_replace("｛借款人手机号｝", $loanInfo["user"]["telnum"], $contractTpl);
		$contractTpl = str_replace("｛借款金额大写｝", cny($loanInfo["money"]), $contractTpl);
		$contractTpl = str_replace("｛借款金额小写｝", $loanInfo["money"], $contractTpl);
		$contractTpl = str_replace("｛借款期限类型｝", $timeType, $contractTpl);
		$contractTpl = str_replace("｛借款利息｝", floatval($loanInfo["interest"]), $contractTpl);
		$contractTpl = str_replace("｛借款开始日｝", date("Y年m月d日", $loanInfo["start_time"]), $contractTpl);
		$contractTpl = str_replace("｛借款结束日｝", date("Y年m月d日", $endTime), $contractTpl);
		$contractTpl = str_replace("｛借款人用户名｝", $loanInfo["user"]["telnum"], $contractTpl);
		$contractTpl = str_replace("｛收款银行账号｝", $loanInfo["banknum"], $contractTpl);
		$contractTpl = str_replace("｛收款银行开户行｝", $loanInfo["bankname"], $contractTpl);
		$contractTpl = str_replace("｛逾期利息｝", floatval($loanInfo["overdue"]), $contractTpl);
		
		$contractTpl = str_replace("｛借款人签名｝", $sign, $contractTpl);
		$contractTpl = str_replace("｛公章｝", C("siteCorporate"), $contractTpl);
		$contractTpl = str_replace("｛合同签订日期｝", date("Y 年 m 月 d 日", $loanInfo["add_time"]), $contractTpl);
		$contractTpl = str_replace("｛借款人住所｝", $addessInfo["addess"] . $addessInfo["addessMore"], $contractTpl);
		$this->assign("tpl", $contractTpl);
	 
	}
	public function viewbill()
	{
		$loanorderModel = D("Loanorder");
		$userInfo = $this->isLogin();
		//$list = $loanorderModel->getNoneList($userInfo["id"]);
		$list=$loanorderModel->where(array("uid" => $userInfo["id"]))->find();
		if(empty($list)){
		//	$this->error("暂时无需还款！");
		}
		
		//利息 = $list['money'] * 利息 * 期限
		$hasMoney=floatval($list['money'])*floatval($list['interest'])*intval($list['time']);
		
		$hasMoney=number_format($hasMoney+floatval($list['money']),2);
		
		$this->assign("oldMoney", number_format($list['money']));
		//$list['money']=number_format($list['money'],2);
		$loanbillModel = D("Loanbill");
		//$hasMoney = $loanbillModel->where(array("uid" => $userInfo["id"], "status" => array("in", "0,1")))->sum("money");
		//$hasInterest = $loanbillModel->where(array("uid" => $userInfo["id"], "status" => array("in", "0,1")))->sum("interest");
		//$hasOverdue = $loanbillModel->where(array("uid" => $userInfo["id"], "status" => array("in", "0,1")))->sum("overdue");
		//$this->assign("noneMoney", toMoney($hasMoney + $hasInterest + $hasOverdue));
		
		//$hasMoney=
		$this->viewContracts();
		$this->assign("noneMoney", $hasMoney);
		$this->assign("list", $list);
		$this->display();
	}
	public function repayments(){
		$loanorderModel = D("Loanorder");
		$billId = I("id");
		if (!$billId) {
			$this->error("账单参数有误");
		}
		$bill = $loanorderModel->where(array("oid" => $billId))->find();
		if (!$bill) {
			$this->error("账单不存在");
		}
		$userInfo = $this->isLogin();
		//$billMoney = toMoney($bill["money"] + $bill["interest"] + $bill["overdue"]);
		$billMoney = toMoney($bill["money"] + $bill["overdue"]);
		$hasMoney=floatval($bill['money'])*floatval($bill['interest'])*intval($bill['time']);
		  $money=(floatval($bill['money'])+$hasMoney)/$bill['time'];
		 $payorderModel = D("Payorder");
		 
		$order = $payorderModel->newOrder($userInfo["id"], $money, array($billId,I('num')));
		if (!$order) {
			$this->error("支付订单创建失败");
		}
		//var_dump($bill);
		$this->assign("data", $order);
		$this->display('repayment');		
		//$this->redirect("Pay/alipay", array("order" => $order));
		exit(0);
	}
	public function repayment()
	{
		$billId = I("id");
		if (!$billId) {
			$this->error("账单参数有误");
		}
		
		$payorderModel = D("Payorder");
		$loanbillModel = D("Loanbill");
		$bill = $loanbillModel->where(array("id" => $billId))->find();
		if (!$bill) {
			$this->error("账单不存在");
		}
		if ($bill["status"] == 2 || $bill["status"] == 3) {
			$this->error("当前账单已还款");
		}
		if ($bill["status"] == 4) {
			$this->error("当前账单已失效");
		}
		$userInfo = $this->isLogin();
		//$billMoney = toMoney($bill["money"] + $bill["interest"] + $bill["overdue"]);
		$billMoney = toMoney($bill["money"] + $bill["overdue"]);
		$order = $payorderModel->newOrder($userInfo["id"], $billMoney, array($billId));
		if (!$order) {
			$this->error("支付订单创建失败");
		}
		//var_dump($bill);
		$this->assign("data", $order);
		$this->display();		
		//$this->redirect("Pay/alipay", array("order" => $order));
		exit(0);
	}
	public function repaymentXq()
	{
		$billId = I("id");
		if (!$billId) {
			$this->error("账单参数有误");
		}
		$payorderxqModel = D("Payorderxq");
		$loanbillModel = D("Loanbill");
		$bill = $loanbillModel->where(array("id" => $billId))->find();
		if (!$bill) {
			$this->error("账单不存在");
		}
		if ($bill["status"] == 2 || $bill["status"] == 3) {
			$this->error("当前账单已还款");
		}
		if ($bill["status"] == 4) {
			$this->error("当前账单已失效");
		}
		//var_dump(intval($bill["overdue_xq"]));die;
		if (intval($bill["overdue_xq"]) == 0) {
			
			$this->error("续期费用出错");
		}		
		$userInfo = $this->isLogin();
		//$billMoney = toMoney($bill["money"] + $bill["interest"] + $bill["overdue"]);
		$billMoney = toMoney($bill["overdue_xq"]);
		$order = $payorderxqModel->newOrder($userInfo["id"], $billMoney, array($billId));
		if (!$order) {
			$this->error("支付订单创建失败");
		}
		//var_dump($bill);
		$this->assign("data", $order);
		$this->display();		
		//$this->redirect("Pay/alipay", array("order" => $order));
		exit(0);
	}	
	public function buy()
	{
		$total_fee = I("total_fee");
		if (!$total_fee) {
			$this->error("参数有误");
		}
		$payorderModel = D("Payorder");
		$userInfo = $this->isLogin();
		$billMoney = toMoney($bill["money"] + $bill["interest"] + $bill["overdue"]);
		$order = $payorderModel->newOrder($userInfo["id"], $total_fee);
		if (!$order) {
			$this->error("支付订单创建失败");
		}
		$this->redirect("Pay/alipay", array("order" => $order));
		exit(0);
	}
}